package com.youdoneed.user.privacy;

import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.base.common.JWTUtils;
import com.youdoneed.user.consts.PrivacyConst;
import com.youdoneed.user.consts.UserConst;
import com.youdoneed.user.model.User;
import com.youdoneed.user.vo.UserBasicPrivacy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Ewing
 * @since 2017-04-21
 **/
@RestController
@RequestMapping("/user")
@Api(description = "隐私策略")
public class PrivacyController {

    @Autowired
    private PrivacyService privacyService;

    /**
     * 更新用户基本的隐私设置。
     */
    @ApiOperation("更新用户基本的隐私设置")
    @RequestMapping(value = "/setUserBasicPrivacy", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage setUserBasicPrivacy(@RequestHeader(UserConst.TOKEN_KEY) String token, UserBasicPrivacy userBasicPrivacy) {
        String userId = JWTUtils.getTokenUserId(token);
        userBasicPrivacy.setUserId(userId);
        privacyService.setUserBasicPrivacy(userBasicPrivacy);
        return new ResultMessage();
    }

    /**
     * 设置谁能查看我的资料。
     */
    @ApiOperation("设置谁能查看我的资料")
    @RequestMapping(value = "/setWhoCanSeeMyInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage setWhoCanSeeMyInfo(@RequestHeader(UserConst.TOKEN_KEY) String token, String[] userIds) {
        String userId = JWTUtils.getTokenUserId(token);
        privacyService.setUserPrivacies(PrivacyConst.TYPE_MY_INFO, userId, userIds);
        return new ResultMessage();
    }

    /**
     * 设置我的好友名单对谁可见。
     */
    @ApiOperation("设置我的好友名单对谁可见")
    @RequestMapping(value = "/setWhoCanSeeMyFriends", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage setWhoCanSeeMyFriends(@RequestHeader(UserConst.TOKEN_KEY) String token, String[] userIds) {
        String userId = JWTUtils.getTokenUserId(token);
        privacyService.setUserPrivacies(PrivacyConst.TYPE_MY_FRIENDS, userId, userIds);
        return new ResultMessage();
    }

    /**
     * 获取谁能查看我的资料。
     */
    @ApiOperation("获取谁能查看我的资料")
    @RequestMapping(value = "/getWhoCanSeeMyInfo", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<User>> getWhoCanSeeMyInfo(@RequestHeader(UserConst.TOKEN_KEY) String token) {
        String userId = JWTUtils.getTokenUserId(token);
        return new ResultMessage<>(privacyService.getPrivacyAllowUsers(PrivacyConst.TYPE_MY_INFO, userId));
    }

    /**
     * 获取我的好友名单对谁可见。
     */
    @ApiOperation("获取我的好友名单对谁可见")
    @RequestMapping(value = "/getWhoCanSeeMyFriends", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<List<User>> getWhoCanSeeMyFriends(@RequestHeader(UserConst.TOKEN_KEY) String token) {
        String userId = JWTUtils.getTokenUserId(token);
        return new ResultMessage<>(privacyService.getPrivacyAllowUsers(PrivacyConst.TYPE_MY_FRIENDS, userId));
    }

    /**
     * 从附近的人中移除并且不想再看到TA。
     */
    @ApiOperation("从附近的人中移除并且不想再看到TA")
    @RequestMapping(value = "/removeAndNotSeeAgain", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage removeAndNotSeeAgain(@RequestHeader(UserConst.TOKEN_KEY) String token, String[] userIds) {
        String userId = JWTUtils.getTokenUserId(token);
        privacyService.setUserPrivacies(PrivacyConst.TYPE_NOT_SEE_AGAIN, userId, userIds);
        return new ResultMessage();
    }

    /**
     * 别人是否能查看我的好友。
     */
    @ApiOperation("别人是否能查看我的好友")
    @RequestMapping(value = "/isCanSeeMyFriends", method = {RequestMethod.GET, RequestMethod.POST})
    public ResultMessage<Boolean> isCanSeeMyFriends(String userId, String otherId) {
        return new ResultMessage<>(privacyService.isCanSeeMyFriends(userId, otherId));
    }

}
